package com.lanou.musicproject.adapter;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.DownloadSeekActivity;
import com.lanou.musicproject.activity.MainActivity;
import com.lanou.musicproject.activity.MusicPlayActivity;
import com.lanou.musicproject.bean.MusicResult;
import com.lanou.musicproject.bean.MyMusicList;
import com.lanou.musicproject.suspendservice.FxService;
import com.lanou.musicproject.utils.MyConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;

import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/26.
 */
public class SingerSongAdapter extends RecyclerView.Adapter<SingerSongAdapter.MyViewHolder> {
    private UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");

 private boolean isService = true;
    private Context context;
    private ArrayList<MusicResult> datas;
    // 定义监听接口
    private OnMyItemClickListener listener;

    // 自定义接口
    public interface OnMyItemClickListener {
        void onItemClick(MusicResult m);
    }

    // 监听接口对象的set方法
    public void setListener(OnMyItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * 构造方法
     *
     * @param context
     * @param datas
     */
    public SingerSongAdapter(Context context, ArrayList<MusicResult> datas) {
        this.context = context;
        this.datas = datas;
    }

    /**
     * 加载布局文件,创建item对应的视图
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_list_song, null);

        return new MyViewHolder(itemView);
    }

    /**
     * 给item设置数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (datas != null && datas.size() > 0) {
            // 设置组件内容
            holder.singerName.setText(datas.get(position).getSingerName());
            holder.songName.setText(datas.get(position).getName());
            holder.songlist_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<MyMusicList> lists = new ArrayList<>();
                    for (MusicResult entity : datas) {
                        MyMusicList musicList = new MyMusicList();
                        musicList.setName(entity.getName());
                        musicList.setSinger(entity.getSingerName());

                        if (entity.getAuditionList() != null && entity.getUrlList().size() > 0) {
                            musicList.setUrl(entity.getAuditionList().get(0).getUrl());
                        }
                        musicList.setFrom("网络");
                        lists.add(musicList);
                    }
                    MainActivity.mini_play(lists, position);

                        FxService.mini_play(lists, position);
                    MusicPlayActivity.playIv(lists, position);

                }
            });
            holder.singersong_cardview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    {
                        switch (v.getId()) {
                            case R.id.singersong_cardview:
                                PopupMenu pm = new PopupMenu(context, v);
                                pm.inflate(R.menu.menu_popup);
                                pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                                    @Override
                                    public boolean onMenuItemClick(MenuItem item) {
                                        switch (item.getItemId()) {
                                            case R.id.rank_share://分享
                                                mController.getConfig().setPlatforms(SHARE_MEDIA.QQ, SHARE_MEDIA.SINA, SHARE_MEDIA.QZONE);
                                                mController.openShare((Activity) context, false);
                                                //设置分享内容
                                                setShareContent();
                                                break;
                                            case R.id.rank_collect://收藏
                                                break;
                                            case R.id.rank_download://下载

                                                Intent intent = new Intent(context, DownloadSeekActivity.class);


                                                if (datas.get(0).getAuditionList().get(0).getUrl() != null) {
                                                    String show_link = datas.get(position).getAuditionList().get(0).getUrl();
                                                    String filename = datas.get(position).getName();
                                                    MyConfig.putSharePre(context, "filename", "filename", filename);
                                                    intent.putExtra("show_link", show_link);
                                                    MyConfig.putSharePre(context, "link", "url", show_link);

                                                    Log.i("*************/////", show_link);
                                                    context.startActivity(intent);


                                                } else {
                                                    Toast.makeText(context, "暂无此资源", Toast.LENGTH_LONG).show();
                                                }


                                                break;
                                        }
                                        return false;
                                    }

                                });
                                pm.show();
                                break;
                        }
                    }
                }
            });
            // 记录当前下标,用于点击之后的数据返回
            holder.position = position;
        }
    }

    //分享内容
    private void setShareContent() {

        //配置SSO
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler((Activity) context,
                "100424468", "c7394704798a158208a74ab60104f0ba");
        qZoneSsoHandler.addToSocialSDK();

        mController.setShareContent("友盟社会化组件 (SDK) 让移动应用快速整合社会分享功能.http://www.umeng.com/social");
        UMImage localImage = new UMImage(context, datas.get(0).getPicUrl());
        //设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setShareContent(datas.get(0).getBrief());
        qzone.setTargetUrl(datas.get(0).getUrlList().get(0).getUrl());
        qzone.setTitle(datas.get(0).getSingerName());
        qzone.setShareMedia(localImage);
        mController.setShareMedia(qzone);
        //设置QQ分享内容
        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setShareContent(datas.get(0).getBrief());
        qqShareContent.setTitle(datas.get(0).getUrlList().get(0).getUrl());
        qqShareContent.setShareMedia(localImage);
        qqShareContent.setTargetUrl(datas.get(0).getUrlList().get(0).getUrl());
        mController.setShareMedia(qqShareContent);

        SinaShareContent sinaShareContent = new SinaShareContent();
        sinaShareContent.setShareContent("来自友盟社会化组件（SDK）让移动应用快速整合社交分享功能-新浪微博" +
                "。http://www.umeng.com/social");
        sinaShareContent.setShareImage(new UMImage(context, R.mipmap.a5));
        mController.setShareMedia(sinaShareContent);

    }

    /**
     * 返回长度
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    /**
     * 组件缓存类
     */
    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView songName;
        private TextView singerName;
        private RelativeLayout rootView;
        private int position;
        private CardView singersong_cardview;
        private ImageView singer_play;
        private LinearLayout songlist_item;

        public MyViewHolder(View itemView) {
            super(itemView);

            rootView = (RelativeLayout) itemView.findViewById(R.id.item_song_root);
            songName = (TextView) itemView.findViewById(R.id.describ_list_song);
            singerName = (TextView) itemView.findViewById(R.id.describ_list_author);
            singersong_cardview = (CardView) itemView.findViewById(R.id.singersong_cardview);
            singer_play = (ImageView) itemView.findViewById(R.id.singer_play);
            songlist_item = (LinearLayout) itemView.findViewById(R.id.songlist_item);


            //配置相关平台
            cofigPlatforms();
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        MusicResult result = datas.get(position);
                        listener.onItemClick(result);
                    }
                }
            });
        }


    }

    //配置需要分享的相关平台
    private void cofigPlatforms() {

        //添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        //添加QQ.QZone平台
        addQQQzonePlatform();


    }

    //添加QQ.QZone平台
    private void addQQQzonePlatform() {
        String appId = "100424468";
        String appkey = "c7394704798a158208a74ab60104f0ba";
        //添加QQ支持,并且设置QQ分享内容的target url
        UMQQSsoHandler umqqSsoHandler = new UMQQSsoHandler((Activity) context, appId, appkey);
//        umqqSsoHandler.setTargetUrl(datas.get(0).getUrlList().get(0).getUrl());
        umqqSsoHandler.addToSocialSDK();
        //添加QZone平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler((Activity) context, appId, appkey);
        qZoneSsoHandler.addToSocialSDK();
    }
    public boolean IsService(Context mContext ,String serviceName){
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals("com.lanou.musicproject.suspendservice.FxService")) {
                isService = true;
                break;
            }
        }
        return isService;

    }



}
